


import java.util.Enumeration;
import java.util.List;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Tomasz
 */
public class xml_tree_manipulator {
    
      public void addChildstoNode(NodeList nl, DefaultMutableTreeNode node, int depth,List allNodes)
  {
      for (int i = 0; i< nl.getLength();i++)
      {
          int temp = 0;
if (nl.item(i).getNodeName() == "#text") continue;
          temp = allNodes.indexOf(new xml_node_container(nl.item(i),depth));
            String tempname = nl.item(i).getNodeName(); 
     if (temp >-1)// && ((xml_node_container)allNodes.get(temp)).node.getParentNode().getNodeName().equals(nl.item(i).getParentNode().getNodeName()) )
  {
      ((xml_node_container)allNodes.get(temp)).is_multinode = true;
      continue;
  }
  String t  = nl.item(i).getNodeName();
          node.add(new DefaultMutableTreeNode(nl.item(i).getNodeName()));
          
         if (ma_dzieci(nl.item(i))) {
              allNodes.add(new xml_node_container(nl.item(i), depth,true));
              
         }
       
     else {
             allNodes.add(new xml_node_container(nl.item(i), depth)); 
      }
          if (ma_dzieci(nl.item(i)))
          {
              Node test = nl.item(i).getFirstChild();
              addChildstoNode(nl.item(i).getChildNodes(),(DefaultMutableTreeNode) node.getLastChild(),depth++,allNodes);
          }
      }
  }
  

  public boolean ma_dzieci(Node node)
  {
      NodeList nl = node.getChildNodes();
      for (int i = 0;i<nl.getLength();i++)
      {
          short tesme = Node.TEXT_NODE;
          if (nl.item(i).getNodeType() != Node.TEXT_NODE)
          {
              return true;
          }
      }
      return false;
  }
  
  
    public void reload_scan_xml(JTree jTree1) {
        DefaultTreeModel model = (DefaultTreeModel) jTree1.getModel();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
        String xml = "<Opis scenariusza>" + "\n";
        for (int i = 0; i < root.getChildCount(); i++) {

            DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(i);
            if (node.getChildCount() > 0) {
                for (int j = 0; j < node.getChildCount(); j++) {
                    DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(j);
                    if (child.getUserObject().toString().contains("autora")) {
                    } else {
                    }
                    
                }
            }
        }
        xml += "<Opis scenariusza>" + "\n";

    }

    
 
    public void searchNode_and_Replace(String nodeStr, String repl, JTree jTree1) {
        DefaultTreeModel model = (DefaultTreeModel) jTree1.getModel();
        DefaultMutableTreeNode m_rootNode = (DefaultMutableTreeNode) model.getRoot();
        DefaultMutableTreeNode node = null;


        Enumeration e = m_rootNode.breadthFirstEnumeration();
        while (e.hasMoreElements()) {
            node = (DefaultMutableTreeNode) e.nextElement();
            if (nodeStr.equals(node.getUserObject().toString())) {
                node.setUserObject(repl);
                model.reload(node);
            }

        }
    }

    public void searchNode_and_add_child(String nodeStr, String newnodename, JTree jTree1) {


        DefaultTreeModel model = (DefaultTreeModel) jTree1.getModel();
        DefaultMutableTreeNode m_rootNode = (DefaultMutableTreeNode) model.getRoot();
        DefaultMutableTreeNode node = null;

        Enumeration e = m_rootNode.breadthFirstEnumeration();
        while (e.hasMoreElements()) {
            node = (DefaultMutableTreeNode) e.nextElement();
            if (nodeStr.equals(node.getUserObject().toString())) {
                node.add(new DefaultMutableTreeNode(newnodename));

                model.reload(node);

            }
        }
    }

    public void searchNode_and_add_child(String nodeStr, DefaultMutableTreeNode newnode, JTree jTree1) {


        DefaultTreeModel model = (DefaultTreeModel) jTree1.getModel();
        DefaultMutableTreeNode m_rootNode = (DefaultMutableTreeNode) model.getRoot();
        DefaultMutableTreeNode node = null;

        Enumeration e = m_rootNode.breadthFirstEnumeration();
        while (e.hasMoreElements()) {
            node = (DefaultMutableTreeNode) e.nextElement();
            if (nodeStr.equals(node.getUserObject().toString())) {
                node.add(newnode);

                model.reload(node);

            }
        }
    }
    
        public void searchNode_and_insert_child(String nodeStr, DefaultMutableTreeNode newnode,int pos, JTree jTree1) {


        DefaultTreeModel model = (DefaultTreeModel) jTree1.getModel();
        DefaultMutableTreeNode m_rootNode = (DefaultMutableTreeNode) model.getRoot();
        DefaultMutableTreeNode node = null;

        Enumeration e = m_rootNode.breadthFirstEnumeration();
        while (e.hasMoreElements()) {
            node = (DefaultMutableTreeNode) e.nextElement();
            if (nodeStr.equals(node.getUserObject().toString())) {
                node.insert(newnode, pos);

                model.reload(node);

            }
        }
    }
        
           public void searchNode_and_insert_child_after(String nodeStr,String afterNode, String newnode, JTree jTree1 ) {


        DefaultTreeModel model = (DefaultTreeModel) jTree1.getModel();
        DefaultMutableTreeNode m_rootNode = (DefaultMutableTreeNode) model.getRoot();
        DefaultMutableTreeNode node = null;
int pos = -1;
        Enumeration e = m_rootNode.breadthFirstEnumeration();
        while (e.hasMoreElements()) {
            node = (DefaultMutableTreeNode) e.nextElement();
            if (nodeStr.equals(node.getUserObject().toString())) {
                
                for (int i = 0; i< node.getChildCount();i++)
                {
                    DefaultMutableTreeNode temp = (DefaultMutableTreeNode) node.getChildAt(i);
                    String chuj = temp.getUserObject().toString();
                    if (temp.getUserObject().toString() == afterNode){     
                     pos = i;
                    }
                }
                node.insert(new DefaultMutableTreeNode(newnode),pos);

            }
        }
           }
    
             public int searchNode_and_return_pos(String nodeStr, String chiid, JTree jTree1) {

                  DefaultMutableTreeNode node2 = new DefaultMutableTreeNode(chiid);

        DefaultTreeModel model = (DefaultTreeModel) jTree1.getModel();
        DefaultMutableTreeNode m_rootNode = (DefaultMutableTreeNode) model.getRoot();
        DefaultMutableTreeNode node = null;

        Enumeration e = m_rootNode.breadthFirstEnumeration();
        while (e.hasMoreElements()) {
            node = (DefaultMutableTreeNode) e.nextElement();
            if (nodeStr.equals(node.getUserObject().toString())) {
                
                return node.getIndex(node2);

            }
        }
        return -1;
    }
        
    public void searchNode_and_add_grandchild(String nodeStr, String grand_name, int childidx, JTree jTree1) {


        DefaultTreeModel model = (DefaultTreeModel) jTree1.getModel();
        DefaultMutableTreeNode m_rootNode = (DefaultMutableTreeNode) model.getRoot();
        DefaultMutableTreeNode node = null;

        Enumeration e = m_rootNode.breadthFirstEnumeration();
        while (e.hasMoreElements()) {
            node = (DefaultMutableTreeNode) e.nextElement();
            if (nodeStr.equals(node.getUserObject().toString())) {

                DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(childidx);


                child.add(new DefaultMutableTreeNode(grand_name));


                model.reload(node);

            }
        }
    }
    
    
        public void searchNode_and_replace_child(String nodeStr,int idx, String newnodename, JTree jTree1) {



            DefaultTreeModel model = (DefaultTreeModel) jTree1.getModel();
        DefaultMutableTreeNode m_rootNode = (DefaultMutableTreeNode) model.getRoot();
        DefaultMutableTreeNode node = null;
DefaultMutableTreeNode node2 = null;
        Enumeration e = m_rootNode.breadthFirstEnumeration();
        while (e.hasMoreElements()) {
            node = (DefaultMutableTreeNode) e.nextElement();
            if (nodeStr.equals(node.getUserObject().toString())) {
                node2 = (DefaultMutableTreeNode)node.getChildAt(idx);
                node2.setUserObject(newnodename);
                model.reload(node);

            }
        }
    }
    
    
}
